//
//  HWViewExtension.swift
//  WoWoLiveAssistant
//
//  Created by 王昌健 on 2021/10/28.
//

import UIKit

extension UIView {
    @objc func addCorner(conrners: UIRectCorner , radius: CGFloat) {
        
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
//        maskLayer.lineWidth = 1
//        maskLayer.borderColor = UIColor.red.cgColor
//        maskLayer.strokeColor = UIColor.red.cgColor
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
        
//        self.layer.mask?.masksToBounds = true
//        self.layer.masksToBounds = true
    }
    
    func addCornerAndBorder(conrners: UIRectCorner , radius: CGFloat, borderWidth:CGFloat,borderColor:UIColor) {
        
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
        self.layer.mask = maskLayer
//        UIBezierPath(roundedRect: bgView.bounds,height: 25)).CGPath
        // Add border
        let borderLayer = CAShapeLayer()
        borderLayer.path = maskLayer.path // Reuse the Bezier path
        borderLayer.fillColor = UIColor.clear.cgColor
        borderLayer.strokeColor = borderColor.cgColor
        borderLayer.lineWidth = borderWidth
        borderLayer.frame = self.bounds
        self.layer.addSublayer(borderLayer)
    }
}

extension String {
    //Range转换为NSRange
    func toNSRange(_ range: Range<String.Index>) -> NSRange {
        guard let from = range.lowerBound.samePosition(in: utf16), let to = range.upperBound.samePosition(in: utf16) else {
            return NSMakeRange(0, 0)
        }
        return NSMakeRange(utf16.distance(from: utf16.startIndex, to: from), utf16.distance(from: from, to: to))
    }
    
    //NSRange转换为Range
    func toRange(_ range: NSRange) -> Range<String.Index>? {
        guard let from16 = utf16.index(utf16.startIndex, offsetBy: range.location, limitedBy: utf16.endIndex) else { return nil }
        guard let to16 = utf16.index(from16, offsetBy: range.length, limitedBy: utf16.endIndex) else { return nil }
        guard let from = String.Index(from16, within: self) else { return nil }
        guard let to = String.Index(to16, within: self) else { return nil }
        return from ..< to
    }
    
}

extension UIView{
    //MARK:- 绘制虚线
    func drawDashLine(_ lineView:UIView,strokeColor: UIColor, lineWidth: CGFloat = 1, lineLength: Int = 10, lineSpacing: Int = 5) {
        let shapeLayer = CAShapeLayer()
        shapeLayer.bounds = lineView.bounds
        shapeLayer.position = CGPoint(x: lineView.frame.width / 2, y: lineView.frame.height / 2)
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.strokeColor = strokeColor.cgColor
        
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = .round
        
        shapeLayer.lineDashPhase = 0
        //每一段虚线长度 和 每两段虚线之间的间隔
        shapeLayer.lineDashPattern = [NSNumber(value: lineLength), NSNumber(value: lineSpacing)]
        
        let path = CGMutablePath()
//        let y = lineView.layer.bounds.height - lineWidth
//        let x = lineView.layer.bounds.width - lineWidth
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: lineView.frame.width, y: lineView.layer.bounds.height))
        shapeLayer.path = path
        lineView.layer.addSublayer(shapeLayer)
    }
    
}


